﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class TextureKit : MonoBehaviour
    {

        public TextureIcon iconType;
        public UITexture texture;

        private string m_textureName;
        public string textureName
        {
            get
            {
                return m_textureName;
            }
            set
            {
                SetTexture(value);
            }
        }

        public string resUrl = "";

        private void SetTexture(string resName)
        {
            string path = "";
            switch (iconType)
            {
                case TextureIcon.HERO:
                    path = "HeroHeader/" + resName;
                    break;
                case TextureIcon.EQUIP:
                    path = "EquipIcons/" + resName;
                    break;
                case TextureIcon.TREASURE:
                    path = "TreasureIcons/" + resName;
                    break;
                case TextureIcon.SKILL:
                    path = "SkillIcons/" + resName;
                    break;
                case TextureIcon.ITEM:
                    path = "ItemIcons/" + resName;
                    break;
                case TextureIcon.TREA2D:
                    path = "Treasure2d/" + resName;
                    break;
                case TextureIcon.FISH:
                    path = "FishIcon/" + resName;
                    break;
                case TextureIcon.MINE:
                    path = "MineIcon/" + resName;
                    break;
                case TextureIcon.DAILYTASK:
                    path = "DailyTaskIcon/" + resName;
                    break;
                case TextureIcon.UNIONPRAY:
                    path = "RushAddIcon/" + resName;
                    break;
                case TextureIcon.UNIONLOGO:
                    path = "UnionLogo/" + resName;
                    break;
                case TextureIcon.PET:
                    path = "Pet/" + resName;
                    break;
                default:
                    return;
            }
            m_textureName = resName;
            if (!string.IsNullOrEmpty(this.resUrl) && this.resUrl == path)
            {
                return;
            }
            UnloadTexture();
            this.resUrl = path;
            if (gameObject.activeInHierarchy)
            {
                LoadTexture();
            }

            //IconManager.Instance.SetUITexture(texture, path);
        }

        int loadCount = 0;

        void OnEnable()
        {
            LoadTexture();
        }

        void OnDisable()
        {
            StopAllCoroutines();
            UnloadTexture();
        }


        private void LoadTexture()
        {
            if (!string.IsNullOrEmpty(resUrl))
            {
                if (texture.mainTexture != null)
                {
                    Debug.LogError("Warning!! Have Not Unload Texture: " + texture.mainTexture.name + " gameObject: " + gameObject.name);
                }
                LoadTexture0();
                //Debug.Log("<color=red> name:" + gameObject.name + "resUrl:" + resUrl + " count:" + loadCount + "</color>");
            }

#if UNITY_EDITOR
            if (loadCount > 1)
            {
                Debug.LogError("Warning!! Loading Count Error, Count:" + loadCount);
            }
#endif
        }

        void LoadTexture0()
        {
            StartCoroutine(_LoadTexture());
            loadCount++;

        }

        void UnloadTexture0()
        {
            texture.mainTexture = null;
            AssetLoad.AssetBundleManager.Instance.UnloadAssetBundle(resUrl);
            loadCount--;
        }

        IEnumerator _LoadTexture()
        {
            AssetLoad.AssetBundleManager.Instance.LoadAssetBundle(resUrl);
            while (!AssetLoad.AssetBundleManager.Instance.IsLoadedAssetBundle(resUrl))
            {
                yield return 1;
            }
            if (texture != null)
            {
                texture.mainTexture = AssetLoad.AssetBundleManager.Instance.GetAssetFromBundle(resUrl, CommonMethod.GetAssetNameFromAssetBundleName(resUrl), typeof(Texture2D)) as Texture2D;
            }
        }

        private void UnloadTexture()
        {
            if (!string.IsNullOrEmpty(resUrl) && loadCount > 0)
            {
                UnloadTexture0();
                //Debug.Log("<color=yellow> name:" + gameObject.name + "resUrl:" + resUrl + " count:" + loadCount + "</color>");
            }
#if UNITY_EDITOR
            if (loadCount != 0)
            {
                Debug.LogError("Warning!! Unloading Count Error, Count:" + loadCount + " gameObject:" + gameObject.transform.parent.name);
            }
#endif
        }
    }
}